<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    /**
     * @method 封装ajax
     * @param{Object}config 携带的参数为
     * methods,发出请求的方法
     * url,请求地址
     * data,请求携带的数据
     * contentType,请求格式
     * @return{Promise} 返回的异步处理
     */
    function ajax(config) {
      let baseUrl = 'http://www.bufantec.com',
        methods = config.methods || 'GET',
        url = config.url || '',
        data = config.data || null,
        contentType = config.contentType || 'application/x-www-form-urlencoded'
        xhr = new XMLHttpRequest();

      methods = methods.toUpperCase();
      return new Promise((resolve, reject) => {
        xhr.onload = function(){
          var res = JSON.parse(xhr.responseText);
          resolve(res)
        }
        xhr.onerror = function () {
          reject(res)
        }
        xhr.open(methods,baseUrl+url,true);
        if(methods==='POST'){
          xhr.setRequestHeader("content-type", contentType);
        }

        xhr.send(data)
      })
    }

    function getData() {
      return ajax({
        url:'/api/douban/movie/coming_soon',
      })
    }

    getData().then(res=>{
      console.log(res);
    })
  </script>
</body>

</html>